import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/api/request.js'
Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		token: ""
	},
	mutations: {
		login(state, token) {
			state.hasLogin = true;
			state.token = token;
		},
		logout(state) {
			state.hasLogin = false
			state.token = ''
		}
	},
    getters:{
        // currentColor(state){
        //     return state.colorList[state.colorIndex]
        // }
    },
	actions: {
		// lazy loading openid
		login: async function ({
			commit
		},userInfo) {
			const {phoneEmail,password}=userInfo
	
			return await new Promise((resolve, reject) => {
				request({ url : "S-Authorization/login",
						method : "POST",
						data : {phoneEmail,password},
						hideLoading : true,
						success:function (res) {
							const {token,name}=res.data
							uni.setStorageSync('token', JSON.stringify(token))
							commit('login',token)
							resolve(res)
					}})
			})
		}
	}
})

export default store
